package com.example.demo.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import com.example.demo.domain.Person;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/information")
public class InformationController {
	@GetMapping
	public String showDesignForm(Model model) {
		model.addAttribute("information",new Person());
		return "information";
	}

	@PostMapping
	public String processDesing(@Valid @ModelAttribute("information") Person information,Errors errors,Model model,
			HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		if (errors.hasErrors()) {
			return "information";
		}
		formSubmit(request,response);
		log.info("Processing information:"+information);
		return "redirect:/select";
		
		
	}
	 protected void formSubmit(HttpServletRequest request,
	            HttpServletResponse response)
	            throws Exception {
		 response.setCharacterEncoding("UTF=8");
	        response.setContentType("text/html;charset=UTF-8");
	        //使用request对象的getSession()获取session，如果session不存在则创建一个
	        HttpSession session = request.getSession();
	        String values=request.getParameter("name");
	        session.setAttribute("name",values);
	        values=request.getParameter("number");
	        session.setAttribute("number",values);
	        values=request.getParameter("year");
	        session.setAttribute("year",values);
	        values=request.getParameter("major");
	        session.setAttribute("major",values);
	        values=request.getParameter("college");
	        session.setAttribute("college",values);
	    }

}